package com.fa.production.controller;

import cn.hutool.core.lang.tree.Tree;
import com.alibaba.excel.EasyExcel;
import com.fa.common.annotation.Anonymous;

import com.fa.common.core.controller.BaseController;
import com.fa.common.core.domain.AjaxResult;
import com.fa.common.core.page.TableDataInfo;
import com.fa.production.domain.MdClient;
import com.fa.production.domain.Processs;
import com.fa.production.domain.Workorder;
import com.fa.production.service.WorkorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/system/workorder")
public class WorkorderController extends BaseController {
    @Autowired
    private WorkorderService workorderService;

    /**
     * 获取所有流程
     *
     * @return
     */
    @RequestMapping("/list")
    public AjaxResult workorder(Workorder process) {
        return AjaxResult.success(workorderService.getlists(process));
    }

    @RequestMapping("/treelist")
    public AjaxResult workorder() {
        //startPage();
        List<Tree<String>> getlist = workorderService.getlist();
        return AjaxResult.success(getlist);
    }

    // 删除流程
    @DeleteMapping(value = "/{workorderId}")
    public AjaxResult remove(@PathVariable Integer workorderId) {
        return AjaxResult.success(workorderService.deleteProcesssById(workorderId));
    }

    @PostMapping()
    public AjaxResult add(@Validated @RequestBody Workorder workorder) {
        return toAjax(workorderService.insertWorkorder(workorder));
    }

    @GetMapping("logTotal")
    public AjaxResult logTotal(){
    	return AjaxResult.success(workorderService.logTotal());
    }
    @PostMapping("getMdItemList")
    public AjaxResult getMdItemList(@RequestBody MdClient mdClient){
        System.out.println(mdClient);
        return AjaxResult.success(workorderService.getMdItemList(mdClient));

    }
    @PostMapping("/export")
    public void exportUnit(HttpServletResponse response) throws Exception {
        System.out.println("开始wqe导出----------------------------------");

        List<Workorder> list =workorderService.getlists(new Workorder());

        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("生产工单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), Workorder.class).sheet("生产工单列表").doWrite(list);
    }
}
